var proof = require('../../../utils/proof.js');
var constract = require('../../../utils/constract.js');
var app = getApp();
Page({
  data: {
    flag:true,
    mes:'获取验证码',
    yzm: false,
    imgUrl: ''
  },
  onLoad: function (options) {
    var cardId = options.cardId;
    this.setData({
      cardId: cardId
    })
  },
  phone: function(e) {
    this.data.phone=e.detail.value;
  },
  code:function(e){
    this.data.code = e.detail.value;
  },
  yzmcode: function(e) {
    this.data.yzmcode = e.detail.value;
  },
  bindcode: function(){
    var that=this;
    var phone=this.data.phone;
    if (!proof.isMobile(phone)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号',
        showCancel: false
      });
      return;     
    };

    this.setData({
      yzm: true,
      imgUrl: constract.verifyCode + '?capName=' + phone + '&t=' + new Date().getTime()
    })
  },
  confirm:function() {//点击确定
    var that = this;
    var phone = this.data.phone;
    var yzmcode = this.data.yzmcode;
    wx.request({
      url: constract.sendCode,
      data: { captcha: yzmcode, phone: phone },
      success: function (res) {
        var contentTxt = res.data.msg;
        if(res.data.code == "12000") {
          if (wx.showToast) {
            wx.showToast({
              title: "验证码已发送",
              icon: "success",
              mask: true,
              duration: 2000
            })
          }
          proof.settime(that);
        } else {
          if (wx.showModal) {
            wx.showModal({
              title: "提示",
              content: contentTxt,
              showCancel: false,
            })
          }
        }
      }
    })
    that.setData({
      yzm: false
    })
  },
  recode:function(e) {//点击验证码更换
    var phone = this.data.phone;
    this.setData({
      imgUrl: constract.verifyCode+'?capName=' + phone + '&t=' + new Date().getTime()
    })
  },
  cancel: function() {//点击取消
    this.setData({
      yzm: false
    })
  },
  submit:function(e){
    var that=this;
    var data = e.detail.value;

    var phone = data.phone;
    var code = data.code;
    data.cardId = this.data.cardId;

    if (!proof.isMobile(phone)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号',
        showCancel: false
      });
      return;
    };
    if (!code) {
      wx.showModal({
        title: '提示',
        content: '请获取验证码',
        showCancel: false
      });
      return;
    };
    app.Verification(function (token) {
      wx.request({
        url: constract.updatePhone,
        header: { token: token },
        data:data,
        success: function (res) {

          if(res.data.msg=="验证码错误"){
            wx.showModal({
              title: '提示',
              content: '验证码错误',
              showCancel: false
            });
            return;
          }
          if(res.data.code=="14000") {
            wx.showModal({
              title: '提示',
              content: '验证码错误次数过多,请重新获取',
              showCancel: false
            });
            return;
          }
          wx.navigateBack();
        }
      });
    })
    
  }
})